// import UIManager from "../../manager/UIManager";
// import UIConst from "../../const/UIConst";
import LoginModel from "../../model/LoginModel";
const {ccclass, property} = cc._decorator;

@ccclass
export default class Property extends cc.Component {

    @property(cc.Button)
    btn_Weapon: cc.Button = null;
    @property(cc.Node)
    ExpBar: cc.Node = null;
    private _personData = LoginModel.instance.personData;

    private SetProperty(nodeName:string,properties:Number){
        return this.node.getChildByName(nodeName).getComponent(cc.Label).string=(properties.toString())
    }
    private SetExp(nodeName:string,CurExp:Number,UpExp){
        return this.node.getChildByName(nodeName).getComponent(cc.Label).string=(CurExp.toString()+" / "+UpExp.toString())
    }
    public getInfo(usercount:string){
        return JSON.parse(cc.sys.localStorage.getItem(usercount))
    }
    private SetButtonGray(){
        this.node.getChildByName('Attack_point').color=cc.color(100,100,100);
        this.node.getChildByName('Defence_point').color=cc.color(100,100,100);
        this.node.getChildByName('Magic_point').color=cc.color(100,100,100);
    }
    private InitButtonColor(){
        this.node.getChildByName('Attack_point').color=cc.color(255,255,255);
        this.node.getChildByName('Defence_point').color=cc.color(255,255,255);
        this.node.getChildByName('Magic_point').color=cc.color(255,255,255);
    }
    private SetTransitionNone(){
        this.node.getChildByName('Attack_point').getComponent(cc.Button).transition=cc.Button.Transition.NONE
        this.node.getChildByName('Defence_point').getComponent(cc.Button).transition=cc.Button.Transition.NONE
        this.node.getChildByName('Magic_point').getComponent(cc.Button).transition=cc.Button.Transition.NONE
    }
    private SetTransitionSprite(){
        this.node.getChildByName('Attack_point').getComponent(cc.Button).transition=cc.Button.Transition.SCALE
        this.node.getChildByName('Defence_point').getComponent(cc.Button).transition=cc.Button.Transition.SCALE
        this.node.getChildByName('Magic_point').getComponent(cc.Button).transition=cc.Button.Transition.SCALE
    }
    private PlusPoint(buttName:string,nodeName:string,property:string){
        var userData = this.getInfo('userData');
        if(userData.allocate>=1){
            this.SetTransitionSprite()//   'Magic_point'
            this.InitButtonColor()
            this.SetProperty(nodeName,++userData.attack);//Attack attack
             this.SetProperty("Allocate",--userData.allocate);
             if(userData.allocate==0){
                this.SetTransitionNone()
                this.SetButtonGray()
             }
        }
        else{
            this.SetTransitionNone()
            this.SetButtonGray()
        }
        cc.sys.localStorage.setItem('userData', JSON.stringify(userData));
    }
    //初始化属性表
    private initPropertySheet(){
        //读取文件
        var userData = this.getInfo('userData');
        this.SetProperty("Attack",userData.attack);
        this.SetProperty("Defence",userData.defence);
        this.SetProperty("Magic",userData.mp);
        this.SetProperty("Rank",userData.rank);
        //this.SetProperty("Allocate",userData.allocate);？？？？？一用这个函数就报错 用下面那个才可以
        this.node.getChildByName("Allocate").getComponent(cc.Label).string=(userData.allocate.toString())
        this.SetExp("Exp",userData.exp,userData.exp>1?userData.rank*500:500);
        this.ExpBar.getComponent(cc.ProgressBar).progress= parseFloat((userData.exp/(userData.rank*500)).toFixed(1))
        if(userData.allocate<=0){
            this.SetButtonGray();
        }
    }
    private AttackPlus() {//buttName nodeName  property
        this.PlusPoint('Attack_point',"Attack",'attack')
    }
    private DefencePlus() {
        this.PlusPoint('Defence_point',"Defence",'defence')
    }
    private MagicPlus() {
        this.PlusPoint('Magic_point',"Magic",'mp')
    }
    start () {
        this.initPropertySheet();
    }

    // update (dt) {}
}
